//
//  DHHomeListViewModel.m
//  宏达APP
//
//  Created by DIANGHONG on 2018/5/3.
//  Copyright © 2018年 123. All rights reserved.
//

#import "DHHomeListViewModel.h"
#import "DHNetworkManager+DHHodaNetworkManager.h"
#import <YYModel.h>
#import <SDWebImage/UIImageView+WebCache.h>
#import <SDWebImage/SDWebImageDownloader.h>

#define kAdv_ListHeight 160
#define kHome2Height 140
#define kGoodItemSmallHeight 100
#define kGoodItemLargeHeight 150

@implementation DHHomeListViewModel

- (void)loadHomeGoodListWithFinishedBlock:(void(^)(BOOL isSuccess))finishedBlock{
    
    [DHNetworkManager.shareManager loadHomeDataWithCompetion:^(NSArray *arrayList, BOOL isSuccess) {
        
        self.homeList = arrayList;
        
        finishedBlock(isSuccess);
    }];
    
    
}



#pragma mark: - 计算cell的高度
- (CGFloat)calcRowHeightWithDict:(NSDictionary *)dict idx:(NSInteger)idx{
    
    if ([dict.allKeys.firstObject isEqualToString:@"adv_list"]) {
        return kAdv_ListHeight;
    }
    if ([dict.allKeys.firstObject isEqualToString:@"navigation"]) {
        return 64;
    }
    if ([dict.allKeys.firstObject isEqualToString:@"home2"]) {
        return kHome2Height;
    }
    if ([dict.allKeys.firstObject isEqualToString:@"home1"]) {
        
        UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:dict[@"home1"][@"image"]]]];
       
        return image.size.height * 0.8;
    }
    if ([dict.allKeys.firstObject isEqualToString:@"goods"]) {
        
        NSArray *array = dict[@"goods"][@"item"];
        
        CGFloat numOfRow = (array.count % 2 == 0) ? array.count / 2 : array.count / 2 + 1;
       
        return array.count > 6 ? kGoodItemSmallHeight * numOfRow : kGoodItemLargeHeight * numOfRow;
    }

    return 0;
}



@end
